package com.robot.orders.controller;

import com.alibaba.fastjson.JSON;
import com.robot.orders.pojo.Purchase;
import com.robot.orders.service.PurchaseService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * purchase控制器层
 *
 * @author lin
 */
@RestController
@CrossOrigin
@RequestMapping("/purchase")
public class PurchaseJsonController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "third/findPurchasesBySearch/{page}/{size}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String thirdFindPurchasesBySearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Purchase> pageList = purchaseService.findSearch(searchMap, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 通过agentId查询purchase列表并分页
     *
     * @param agentId
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findPurchasesByAgentIdAndDeletedAtIsNullOrderByCreatedAtDesc/{agentId}/{page}/{size}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindPurchasesByAgentIdAndDeletedAtIsNullOrderByCreatedAtDesc(@PathVariable String agentId, @PathVariable int page, @PathVariable int size) {
        Page<Purchase> pageList = purchaseService.findAllByAgentIdAndDeletedAtIsNullOrderByCreatedAtDesc(agentId, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 通过agentId和status查询purchase列表并分页
     *
     * @param agentId
     * @param status
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findPurchasesByAgentIdAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc/{agentId}/{status}/{page}/{size}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindPurchasesByAgentIdAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc(@PathVariable String agentId, @PathVariable String status, @PathVariable int page, @PathVariable int size) {
        Page<Purchase> pageList = purchaseService.findAllByAgentIdAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc(agentId, status, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 通过agentId和id查询purchase列表并分页
     *
     * @param id
     * @param agentId
     * @return
     */
    @RequestMapping(value = "third/findPurchaseByIdAndAgentId/{id}/{agentId}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindPurchaseByIdAndAgentId(@PathVariable String id, @PathVariable String agentId) {
        return JSON.toJSONString(purchaseService.findByIdAndAgentId(id, agentId));
    }


    /**
     * 通过storeId查询purchase列表并分页
     *
     * @param storeId
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findPurchasesByStoreIdAndDeletedAtIsNullOrderByCreatedAtDesc/{storeId}/{page}/{size}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindPurchasesByStoreIdAndDeletedAtIsNullOrderByCreatedAtDesc(@PathVariable String storeId, @PathVariable int page, @PathVariable int size) {
        Page<Purchase> pageList = purchaseService.findAllByStoreIdAndDeletedAtIsNullOrderByCreatedAtDesc(storeId, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 通过storeId和status查询purchase列表并分页
     *
     * @param storeId
     * @param status
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findPurchasesByStoreIdAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc/{storeId}/{status}/{page}/{size}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindPurchasesByStoreIdAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc(@PathVariable String storeId, @PathVariable String status, @PathVariable int page, @PathVariable int size) {
        Page<Purchase> pageList = purchaseService.findAllByStoreIdAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc(storeId, status, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 通过storeId和id查询purchase列表并分页
     *
     * @param id
     * @param storeId
     * @return
     */
    @RequestMapping(value = "third/findPurchaseByIdAndStoreId/{id}/{storeId}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindPurchaseByIdAndStoreId(@PathVariable String id, @PathVariable String storeId) {
        return JSON.toJSONString(purchaseService.findByIdAndStoreId(id, storeId));
    }

    /**
     * 根据storeId查找剩余余额
     *
     * @param storeId
     * @return
     */
    @RequestMapping(value = "third/findPurchaseBalanceByStoreId/{storeId}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindPurchaseBalanceByStoreId(@PathVariable String storeId) {
        return JSON.toJSONString(purchaseService.findBalanceByStoreId(storeId));
    }

    /**
     * 增加采购单
     *
     * @param purchase
     */
    @RequestMapping(value = "third/addPurchase", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public void thirdAddPurchase(@RequestBody String purchase) {
        purchaseService.add(JSON.parseObject(purchase, Purchase.class));
    }

    /**
     * 修改采购单
     *
     * @param purchase
     */
    @RequestMapping(value = "third/updatePurchase", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
    public void thirdUpdatePurchase(@RequestBody String purchase) {
        purchaseService.update(JSON.parseObject(purchase, Purchase.class));
    }

    /**
     * 删除采购单
     *
     * @param id
     */
    @RequestMapping(value = "third/deletePurchase/{id}", method = RequestMethod.DELETE, produces = "application/json; charset=utf-8")
    public void thirdDeletePurchase(@PathVariable String id) {
        purchaseService.deleteById(id);
    }
}